home *** CD-ROM | disk | FTP | other *** search
/ Maplin Electronics Catalogue 2001 Winter / Maplins Catalogue Winter 2001.iso / JScript / Search.js < prev    next >
Text File  |  2001-12-11  |  4KB  |  125 lines

  1. <!--
  2. function Page(url,title,keywords,description) {
  3.  while ((url.length > 0) && (url.charAt(0) == " ")) {
  4.   url = url.substring(1,url.length);
  5.  }
  6.  this.url = url;
  7.  while ((title.length > 0) && (title.charAt(0) == " ")) {
  8.   title = title.substring(1,title.length);
  9.  }
  10.  this.title = title;
  11.  this.keywords = keywords;
  12.  this.description = description;
  13.  return this;
  14. }
  15. function Database() {
  16.  var pos = 0;
  17.  while ((pos1 = amorphous.indexOf("~",pos)) != -1) {
  18.   pos2 = amorphous.indexOf("|",pos1+1);
  19.   pos3 = amorphous.indexOf("^",pos2+1);
  20.   pos4 = amorphous.indexOf("*",pos3+1);
  21.   if ((pos2 != -1)
  22.   && (pos2 < pos3) && (pos3 < pos4)
  23.   && (pos4 <= amorphous.indexOf("*",pos))) {
  24.    this[database_length++] = new Page(amorphous.substring(pos,pos1),
  25.    amorphous.substring(pos1+1,pos2),
  26.    amorphous.substring(pos2+1,pos3),
  27.    amorphous.substring(pos3+1,pos4));
  28.    pos = pos4+1;
  29.   } else { // error reading amorphous database
  30.    if (pos+30 <= amorphous.length)
  31.     alert('Error reading in amorphous database around "'
  32.      + amorphous.substring(pos,pos+30) + '"');
  33.    pos = amorphous.indexOf("*",pos) + 1;
  34.   }
  35.  }
  36.  return this;
  37. }
  38. function search(str) {
  39.  menu_length = 0;
  40.  temp = new Object();
  41.  temp_length = 0;
  42.  words_length = 0;
  43.  words = new Object();
  44.  pos = 0;
  45.  while ((pos = str.indexOf(" ")) != -1
  46.  && and_search != "exact") {
  47.   words[words_length] = str.substring(0,pos);
  48.   if (words[words_length].length > 0)
  49.    words_length++;
  50.   if (str.length == 1)
  51.    str="";
  52.   else
  53.    str = str.substring(pos+1,str.length);
  54.  }
  55.  if (str.length > 0)
  56.   words[words_length++] = str;
  57.  for (q=0;q<words_length;q++) {
  58.   temp_length = 0;
  59.   str = words[q].toLowerCase();
  60.   len = (and_search=="and"&&q>0?menu_length:database_length);
  61.   for (n=0; n<len; n++) {
  62.    if (and_search=="and"&&q>0) {
  63.     combo = (menu[n].title + " " + menu[n].keywords).toLowerCase();
  64.    } else {
  65.     combo = (database[n].title + " "  + database[n].keywords).toLowerCase();
  66.    }
  67.    if (combo.indexOf(str) != -1) // found
  68.     temp[temp_length++] = (and_search=="and"&&q>0?menu[n]:database[n]);
  69.   }
  70.   if (and_search!="and" && q>0) {
  71.    added = 0;
  72.    for (i=0;i<temp_length;i++) {
  73.     duplicate = false;
  74.     for (j=0;j<menu_length&&!duplicate;j++) {
  75.      if (menu[j] == temp[i]) {
  76.       duplicate = true;
  77.      }
  78.     }
  79.     if (!duplicate)
  80.      menu[menu_length+(added++)] = temp[i];
  81.    }
  82.    menu_length += added;
  83.   } else {
  84.    for(h=0;h<temp_length;h++)
  85.     menu[h] = temp[h];
  86.    menu_length = temp_length;
  87.   }
  88.  }
  89. }
  90. function entry() {
  91.  if ((document.entryform.keyword.value.length == 0)
  92.  || (document.entryform.keyword.value == " ")) {
  93.   alert("*****First you must enter a keyword to search for...");
  94.   return false;
  95.  }
  96.  and_search = (document.entryform.and_or.selectedIndex == 0?"and":"or");
  97.  if (document.entryform.and_or.selectedIndex == 2)
  98.   and_search = "exact";
  99.  location.href = location.pathname + "?"
  100.   + escape(document.entryform.keyword.value)
  101.   + (and_search != "or"?"&"+and_search:"");
  102.  return false;
  103. }
  104. function redWord(str) {
  105.  for(r=0; r<words_length; r++) {
  106.   pos = -3;
  107.   word = words[r].toLowerCase();
  108.   while ((pos = str.toLowerCase().indexOf(word,pos+3)) != -1) {
  109.    val = pos+word.length;
  110.    str = str.substring(0,pos) + "*"
  111.     + str.substring(pos,val) + "|"
  112.     + str.substring(val,str.length);
  113.   }
  114.  }
  115.  pos = -16;
  116.  while ((pos = str.toLowerCase().indexOf("*",pos+16)) != -1)
  117.   str = str.substring(0,pos) + "<font color=red>"
  118.    + str.substring(pos+1,str.length);
  119.  pos = -7;
  120.  while ((pos = str.toLowerCase().indexOf("|",pos+7)) != -1)
  121.   str = str.substring(0,pos) + "</font>"
  122.    + str.substring(pos+1,str.length);
  123.  return str;
  124. }
  125.